<?php
    require_once('page_base_jectly.php');

	/*
     * sub classes of page_base_jectly will include only the changes from index_2;
	 */
	class projects extends page_base_jectly {

        protected $category_id;//11-100
        //protected $main_category_id;//1-10
        //protected $category_string;//query_string

        protected $search_type_string;//query_string
        protected $selected_category_index_string;//query_string

        protected $array_search_result;//for projects listing
        protected $table_search_result;//for projects listing

        protected $result_count=0;
        protected $show_items_per_page=20;
        protected $current_page=0;
        protected $result_start=0;
        protected $result_end=0;
        protected $hit_message;

        function __construct() {
            parent::__construct();
            $this->bool_main_menu_on=true;
            $this->bool_main_logo_on=true;
            $this->bool_search_tab_on=true;
            $this->bool_main_3boxes_on=false;
            $this->bool_search_result_within_page_on=false;
            $this->bool_account_menu_on=false;
            $this->bool_contents_area_on=true;
        }

        protected $contents_html='<div class=\'center\'>This is a projects page.</div>';


        protected function Form_Create() {

            $this->check_query_string();

            $this->panel_search_tab_Create();
            $this->panel_menubar_Create();

            if($this->bool_logined())
            {
                $this->pnl_mebubar->hide_all_controls();
            }

            //$this->update_search_result();

            //create projects table
            $this->table_search_result = new QDataGrid($this);
			$this->table_search_result->CellPadding = 5;
			$this->table_search_result->CellSpacing = 0;
            $this->table_search_result->UseAjax = false;

            $this->table_search_result->AddColumn(new QDataGridColumn('Project ID', '<a href="#"><?= $_ITEM->Id ?></a>', 'Width=60',array('OrderByClause' => QQ::OrderBy(QQN::TableProject()->Id), 'ReverseOrderByClause' => QQ::OrderBy(QQN::TableProject()->Id, false))));
            $this->table_search_result->AddColumn(new QDataGridColumn('Title', '<?= $_ITEM->Title ?>', 'Width=100',array('OrderByClause' => QQ::OrderBy(QQN::TableProject()->Title), 'ReverseOrderByClause' => QQ::OrderBy(QQN::TableProject()->Title, false))));
            $this->table_search_result->AddColumn(new QDataGridColumn('Buyer', '<?= $_ITEM->TotalBid ?>', 'Width=70',array('OrderByClause' => QQ::OrderBy(QQN::TableProject()->TotalBid), 'ReverseOrderByClause' => QQ::OrderBy(QQN::TableProject()->TotalBid, false))));
            $this->table_search_result->AddColumn(new QDataGridColumn('PostDate', '<?= $_ITEM->PostDate ?>', 'Width=50',array('OrderByClause' => QQ::OrderBy(QQN::TableProject()->PostDate), 'ReverseOrderByClause' => QQ::OrderBy(QQN::TableProject()->PostDate, false))));

            $this->table_search_result->Paginator = new QPaginator($this);
            $this->table_search_result->PaginatorAlternate = new QPaginator($this);
            $this->table_search_result->ItemsPerPage = $this->show_items_per_page;
            $this->table_search_result->SetDataBinder('bind_search_result');

            //$this->table_search_result->DisplayStyle="block";

            //$this->table_search_result->Paginator->

            // Make the DataGrid look nice
			$objStyle = $this->table_search_result->RowStyle;
			$objStyle->FontSize = 12;

			$objStyle = $this->table_search_result->AlternateRowStyle;
			$objStyle->BackColor = 'lightgray';

			$objStyle = $this->table_search_result->HeaderRowStyle;
			$objStyle->ForeColor = 'black';
			$objStyle->BackColor = 'white';

			// Because browsers will apply different styles/colors for LINKs
			// We must explicitly define the ForeColor for the HeaderLink.
			// The header row turns into links when the column can be sorted.
			$objStyle = $this->table_search_result->HeaderLinkStyle;
			$objStyle->ForeColor = 'black';

            /*$this->table_search_result = new QDataRepeater($this);

            $this->table_search_result->Paginator = new QPaginator($this);
            $this->table_search_result->PaginatorAlternate = new QPaginator($this);
            $this->table_search_result->ItemsPerPage = $this->show_items_per_page;

            $this->table_search_result->UseAjax = false;

            //$this->table_search_result->Template = 'generated/site_data_item.tpl.php';
            $this->table_search_result->SetDataBinder('dtgTableSite_Bind');*/

            if(isset($this->selected_category_index_string))
            {
                $this->set_index_listbox_search_project(intval($this->selected_category_index_string));
            }

            $this->contents_html='SelectedIndex='+$this->pnl_search_tab->listbox_search_project->SelectedIndex.'SelectedValue='+$this->pnl_search_tab->listbox_search_project->SelectedValue;
        }

        public function render_contents_area() {

            if($this->bool_contents_area_on)
            {
                echo($this->contents_html);
                //echo($this->contents_html.' '.sizeof($this->array_search_result));

                echo('<div class=\'center\'>');
                $this->table_search_result->Render();
                echo('</div>');
            }
        }

        protected function check_query_string()
        {
            //check menubar mode
            $this->mode_string_page=QApplication::QueryString('p');

            if(!empty($this->mode_string_page))
            {
                if($this->mode_string_page ==='0')
                {
                    $this->bool_mode_login=true;
                    $this->bool_mode_register=false;
                    $this->bool_mode_forgot=false;
                }
                else if($this->mode_string_page ==='1')
                {
                    $this->bool_mode_login=false;
                    $this->bool_mode_register=true;
                    $this->bool_mode_forgot=false;
                }
                else if($this->mode_string_page ==='2')
                {
                    $this->bool_mode_login=false;
                    $this->bool_mode_register=false;
                    $this->bool_mode_forgot=true;
                }
            }
            else
            {
                $this->bool_mode_login=true;
                $this->bool_mode_register=false;
                $this->bool_mode_forgot=false;
            }

            $this->category_string=QApplication::QueryString('c');//category_id

            $this->selected_category_index_string=QApplication::QueryString('i');//category_list_index

        }
        
        protected function update_search_result()
        {
            if(!empty($this->category_string))
            {
                //1-100
                if(is_numeric($this->category_string))
                {
                    if(intval($this->category_string)>10 && intval($this->category_string)<101)//search by category id
                    {
                        $this->result_count = TableProject::CountOpenProjectsByCategoryId($this->category_string,null);

                        if($this->result_count===0)
                        {
                            $this->array_search_result = array();
                        }
                        else
                        {
                            $this->array_search_result = TableProject::LoadObjectOpenProjectsByCategoryIdOrderByPostDate($this->category_string,null);
                        }
                    }
                    else if(intval($this->category_string)>0 && intval($this->category_string)<11)//search by main category id
                    {
                        $this->result_count = TableProject::CountOpenProjectsByCategoryId($this->category_string,null);

                        if($this->result_count===0)
                        {
                            $this->array_search_result = array();
                        }
                        else
                        {
                            $this->array_search_result = TableProject::LoadObjectOpenProjectsByCategoryIdOrderByPostDate($this->category_string,null);
                        }
                    }
                    else
                    {
                        $this->category_string=null;

                        $this->result_count = TableProject::CountOpenProjectsAll(null);

                        if($this->result_count===0)
                        {
                            $this->array_search_result = array();
                        }
                        else
                        {
                            $this->array_search_result = TableProject::LoadObjectOpenProjectsAllOrderByPostDate(null);
                        }
                    }
                }
                else//show all open projects
                {
                    $this->category_string=null;

                    $this->result_count = TableProject::CountOpenProjectsAll(null);

                    if($this->result_count===0)
                    {
                        $this->array_search_result = array();
                    }
                    else
                    {
                        $this->array_search_result = TableProject::LoadObjectOpenProjectsAllOrderByPostDate(null);
                    }
                }

            }
        }

        protected function bind_search_result() {

            $this->table_search_result->TotalItemCount = TableProject::CountAll();
            //$this->table_search_result->DataSource = TableProject::LoadArrayByCategoryId(intval($this->category_string));

            $this->table_search_result->DataSource = TableProject::LoadAll(QQ::Clause($this->table_search_result->OrderByClause,$this->table_search_result->LimitClause));

            /*$this->table_search_result->TotalItemCount = $this->result_count;

            if($this->result_count===0)
            {
                //$this->searchResult = array();
                $this->table_search_result->DataSource = $this->array_search_result;
            }
            else
            {
                //$this->searchResult = TableSite::LoadArrayBySearchSiteName($this->keyword_site_name,null);
                $this->table_search_result->DataSource = array_slice($this->array_search_result, $this->table_search_result->ItemsPerPage * ($this->table_search_result->PageNumber-1), $this->table_search_result->ItemsPerPage) ;
            }
            

            $this->current_page=$this->table_search_result->Paginator->PageNumber;

            $this->result_start = (($this->table_search_result->PageNumber - 1) * $this->table_search_result->ItemsPerPage) + 1;

            if(($this->result_start + $this->show_items_per_page)<=$this->result_count)
            {
                $this->result_end = $this->result_start + $this->show_items_per_page - 1;
            }
            else
            {
                $this->result_end = $this->result_start + ($this->table_search_result->ItemsPerPage-(($this->table_search_result->PageNumber* $this->table_search_result->ItemsPerPage) - $this->result_count))-1;
            }

            if($this->result_count===1)
            {
                $this->hit_message=$this->result_count.'件ヒット！ '.$this->result_count.'を表示';
            }
            else
            {
                if($this->result_start==$this->result_end)
                {
                    $this->hit_message=$this->result_count.'件ヒット！ '.$this->result_end.'を表示';
                }
                else
                {
                    $this->hit_message=$this->result_count.'件ヒット！ '.$this->result_start.'～'.$this->result_end.'まで表示';
                }
            }*/
        }


	}

    projects::Run('projects','projects.tpl.php');

?>